import os
    import subprocess
    import sys

    def generate_requirements():
        """
        生成当前项目实际使用的依赖到requirements.txt
        使用pipreqs工具，需先安装：pip install pipreqs
        """
        try:
            # 检查pipreqs是否安装
            subprocess.check_output(["pip", "show", "pipreqs"], stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError:
            print("正在安装pipreqs工具...")
            subprocess.check_call([sys.executable, "-m", "pip", "install", "pipreqs"])
        
        # 获取当前目录
        current_dir = os.getcwd()
        print(f"正在分析目录: {current_dir}")
        
        # 生成requirements.txt
        try:
            subprocess.check_call([
                "pipreqs", 
                current_dir, 
                "--encoding=utf8", 
                "--force"
            ])
            print("requirements.txt 已成功生成")
            print(f"文件位置: {os.path.join(current_dir, 'requirements.txt')}")
        except subprocess.CalledProcessError as e:
            print(f"生成失败: {e}")

    if __name__ == "__main__":
        generate_requirements()
    